package com.hatic.money;

import java.util.Map;

import org.jxstar.control.action.RequestContext;
import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.DateUtil;
import org.jxstar.util.key.CodeCreator;
import org.jxstar.util.key.KeyCreator;

/**
 * 投资计划审批通过后，等待打包处理。
 *
 * @author TonyTan
 * @version 1.0, 2011-10-17
 */
public class PlanPassBO extends BusinessObject {
	private static final long serialVersionUID = 1L;
	
	//创建投资计划包
	public String newbao(RequestContext request) {
		String money_type2 = request.getRequestValue("money_plan.money_type2");
		String plan_name = request.getRequestValue("money_plan.plan_name");
		String plan_user = request.getRequestValue("money_plan.plan_user");
		String plan_userid = request.getRequestValue("money_plan.plan_userid");
		String dept_name = request.getRequestValue("money_plan.dept_name");
		String dept_id = request.getRequestValue("money_plan.dept_id");
		String plan_desc = request.getRequestValue("money_plan.plan_desc");
		String year_content2 = request.getRequestValue("money_plan.year_content2");
		
		if (money_type2.length() == 0 || plan_name.length() == 0 ||
				dept_name.length() == 0) {
			setMessage("投资计划包的信息不完整！");
			return _returnFaild;
		}
		
		String[] planIds = request.getRequestValues("keyid");
		if (planIds == null || planIds.length == 0) {
			setMessage("没选择需要合并的投资计划！");
			return _returnFaild;
		}
		
		Map<String,String> plandata = queryPlan(planIds[0]);
		String subdeptid = plandata.get("dept_id").substring(0, 4);
		_log.showDebug("------------subdeptid=" + subdeptid);
		if (!subdeptid.equals(dept_id.substring(0, 4))) {
			setMessage("虚拟包的申报单位与子计划的申报单位不同，不能合并！");
			return _returnFaild;
		}
		//取预算年度与项目类别
		String plan_year = plandata.get("plan_year");
		String money_type = plandata.get("money_type");
		
		//取合计总预算与年度投资
		Map<String,String> mpSum = queryPlanSum(planIds);
		String plan_money = mpSum.get("plan_money");
		String year_money = mpSum.get("year_money");
		if (plan_money.length() == 0 || year_money.length() == 0) {
			setMessage("选择的投资计划合计金额为0，不能合并！");
			return _returnFaild;
		}
		
		//创建计划包的ID与编号
		String plan_id = KeyCreator.getInstance().createKey("money_plan");
		//区分投资计划包的前缀为TB
		String plan_code = CodeCreator.getInstance().
				createTableCode("money_plan", "TB", plan_year, "0000");
		
		//创建投资计划包
		String sql = "insert into money_plan(" +
			"plan_id,plan_code,plan_name,money_type2,dept_name,dept_id," +
			"plan_desc,year_content2,plan_user,plan_userid," +
			"money_type,plan_year," +
			"plan_money,year_money,plan_status,bao_type,plan_type,audit_up,plan_date,up_comp) values(" +
			"?, ?, ?, ?, ?, ?, " + 
			"?, ?, ?, ?, " + 
			"?, ?, " + 
			"?, ?, '25', '1', 'yd', '1', ?, '1')";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(plan_id);
		param.addStringValue(plan_code);
		param.addStringValue(plan_name);
		param.addStringValue(money_type2);
		param.addStringValue(dept_name);
		param.addStringValue(dept_id);
		param.addStringValue(plan_desc);
		param.addStringValue(year_content2);
		param.addStringValue(plan_user);
		param.addStringValue(plan_userid);
		param.addStringValue(money_type);
		param.addStringValue(plan_year);
		param.addDoubleValue(plan_money);
		param.addDoubleValue(year_money);
		param.addDateValue(DateUtil.getToday());
		if (!_dao.update(param)) {
			setMessage("创建投资计划包出错！");
			return _returnFaild;
		}
		
		//修改子计划相关信息
		if (!updateSubPlan(planIds, plan_id, plan_code, money_type2)) {
			setMessage("更新投资计划包的子计划状态出错！");
			return _returnFaild;
		}
		
		return _returnSuccess;
	}

	//单个计划备案，修改需报国家局为是，状态改为待备案
	public String newmemo(String[] planIds) {
		if (planIds == null || planIds.length == 0) {
			setMessage("没有选择投资计划！");
			return _returnFaild;
		}
		
		for (int i = 0, n = planIds.length; i < n; i++) {
			newmemo(planIds[i]);
		}
		
		return _returnSuccess;
	}
	
	//等待授权，修改状态为待授权
	public String newup(String[] planIds) {
		if (planIds == null || planIds.length == 0) {
			setMessage("没有选择投资计划！");
			return _returnFaild;
		}
		
		for (int i = 0, n = planIds.length; i < n; i++) {
			newup(planIds[i]);
		}
		
		return _returnSuccess;
	}
	
	public String delsub(String[] planIds) {
		if (planIds == null || planIds.length == 0) {
			setMessage("没有选择子投资计划！");
			return _returnFaild;
		}
		
		String parentId = getParentId(planIds[0]);
		_log.showDebug(".....................parentId=" + parentId);
		
		//更新子投资计划状态为正常投资计划
		String usql = "update money_plan set plan_status = '22', bao_type = '0', up_comp = '0', " +
				"plan_parentid = '', plan_parentcode = '' where plan_id = ?";
		DaoParam uparam = _dao.createParam(usql);
		for (int i = 0; i < planIds.length; i++) {
			uparam.addStringValue(planIds[i]);
			_dao.update(uparam);
			
			uparam.clearParam();
		}
		
		//重新计算计划包的合计金额
		(new PlanMemoBO()).updateSum(parentId);
		
		return _returnSuccess;
	}
	
	//取子投资计划的父ID
	private String getParentId(String planId) {
		String sql = "select plan_parentid from money_plan where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		Map<String, String> mpData = _dao.queryMap(param);
		return mpData.get("plan_parentid");
	}
	
	//修改需报国家局为是，状态改为待备案
	private boolean newmemo(String planId) {
		String usql = "update money_plan set plan_status = '25', up_comp = '1' where plan_id = ?";
		DaoParam uparam = _dao.createParam(usql);
		uparam.addStringValue(planId);
		
		return _dao.update(uparam);
	}
	
	//修改状态为待授权
	private boolean newup(String planId) {
		String usql = "update money_plan set plan_status = '31', up_comp = '0' where plan_id = ?";
		DaoParam uparam = _dao.createParam(usql);
		uparam.addStringValue(planId);
		
		return _dao.update(uparam);
	}
	
	//取一条子计划的相关字段值
	private Map<String,String> queryPlan(String planId) {
		String sql = "select dept_id, money_type, plan_year from money_plan where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		return _dao.queryMap(param);
	}
	
	//更新子计划的状态，相关父计划的信息，修改子计划为报国家局计划，方便后期报表统计
	private boolean updateSubPlan(String[] planIds, String parentId, String parentCode, String money_type2) {
		String where = arrayToWhere(planIds);
		String sql = "update money_plan set plan_status = '25', bao_type = '6', up_comp = '1', " +
				"plan_parentid = ?, plan_parentcode = ?, money_type2 = ? where plan_id in " + where;
		
		DaoParam uparam = _dao.createParam(sql);
		uparam.addStringValue(parentId);
		uparam.addStringValue(parentCode);
		uparam.addStringValue(money_type2);
		
		return _dao.update(uparam);
	}
	
	//取子计划的合计金额
	private Map<String,String> queryPlanSum(String[] planIds) {
		String where = arrayToWhere(planIds);
		String sql = "select sum(plan_money) as plan_money, sum(year_money) as year_money " +
			"from money_plan where plan_id in " + where;
		DaoParam param = _dao.createParam(sql);
		return _dao.queryMap(param);
	}
	
	//把多个id转换为类似：('a', 'b', 'c') 的字符串
	private String arrayToWhere(String[] astr){
		StringBuilder sb = new StringBuilder();
		for (int i = 0, n = astr.length; i < n; i++) {
			sb.append(astr[i]+"','");
		}
		String tmps = "";
		if (sb.length() > 0) {
			tmps = sb.substring(0, sb.length()-2);
		}
		return "('" + tmps + ")";
	}
}
